#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
符号提取器统一接口
"""

from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Set

from core.utils_module.language_utils import detect_language_for_file


@dataclass
class SymbolInfo:
    """符号信息"""
    name: str
    type: str  # class/method/property/function/struct/enum/protocol/interface
    file: str
    line: int
    column: int
    modifiers: List[str] = field(default_factory=list)  # public/private/static等
    parent: Optional[str] = None  # 父类/父结构
    dependencies: List[str] = field(default_factory=list)  # 依赖的其他符号


@dataclass
class ExtractorResult:
    """提取结果"""
    symbols: Dict[str, List[SymbolInfo]]  # 按类型分组的符号
    errors: List[str] = field(default_factory=list)
    warnings: List[str] = field(default_factory=list)
    metadata: Dict = field(default_factory=dict)

    def get_all_symbol_names(self) -> Set[str]:
        """获取所有符号名称"""
        names = set()
        for symbol_list in self.symbols.values():
            for symbol in symbol_list:
                names.add(symbol.name)
        return names

    def get_symbols_by_type(self, symbol_type: str) -> List[SymbolInfo]:
        """按类型获取符号"""
        return self.symbols.get(symbol_type, [])

    def merge(self, other: 'ExtractorResult'):
        """合并另一个结果"""
        for symbol_type, symbols in other.symbols.items():
            if symbol_type in self.symbols:
                self.symbols[symbol_type].extend(symbols)
            else:
                self.symbols[symbol_type] = symbols
        self.errors.extend(other.errors)
        self.warnings.extend(other.warnings)
        self.metadata.update(other.metadata)


class ISymbolExtractor(ABC):
    """符号提取器统一接口"""

    @abstractmethod
    def extract_symbols(self, file_path: str) -> ExtractorResult:
        """
        提取文件中的符号

        Args:
            file_path: 文件路径

        Returns:
            ExtractorResult: 提取结果
        """
        pass

    @abstractmethod
    def extract_symbols_batch(self, file_paths: List[str]) -> ExtractorResult:
        """
        批量提取符号

        Args:
            file_paths: 文件路径列表

        Returns:
            ExtractorResult: 合并后的提取结果
        """
        pass

    @abstractmethod
    def supports_language(self, language: str) -> bool:
        """
        是否支持该语言

        Args:
            language: 语言标识符（swift/objc/java/kotlin等）

        Returns:
            bool: 是否支持
        """
        pass

    @abstractmethod
    def get_supported_languages(self) -> List[str]:
        """
        获取支持的语言列表

        Returns:
            List[str]: 支持的语言列表
        """
        pass

    def get_file_language(self, file_path: str) -> Optional[str]:
        """
        根据文件扩展名判断语言

        Args:
            file_path: 文件路径

        Returns:
            Optional[str]: 语言标识符，如果不支持则返回None
        """
        language = detect_language_for_file(file_path)
        if language and self.supports_language(language):
            return language
        return None
